package com.ytlz.dmcs.model.system.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ytlz.dmcs.model.system.dao.SysFieldDao;
import com.ytlz.dmcs.model.system.dao.SysObjectDao;
import com.ytlz.dmcs.model.system.entity.SysField;
import com.ytlz.dmcs.model.system.entity.SysObject;

@Controller
@RequestMapping("/admin")
public class AdminView {	
	
	@Autowired
	private SysObjectDao sysObjectDao;
	
	@Autowired
	private SysFieldDao sysFieldDao;
	
	@RequestMapping("user")
	public String user(HttpServletRequest request,Model model) {
		request.setAttribute("datatitle", "用户");
		request.setAttribute("datatype", "sys_user");
		List<Map<String,Object>> attributes = new ArrayList<Map<String,Object>>();
		Map<String,Object> field = new HashMap<String, Object>();
		field.put("name", "username");
		field.put("label", "账号");
		field.put("type", "text");
		attributes.add(field);
		
		field = new HashMap<String, Object>();
		field.put("name", "displayname");
		field.put("label", "用户名");
		field.put("type", "text");
		attributes.add(field);
		
		field = new HashMap<String, Object>();
		field.put("name", "mail");
		field.put("label", "邮箱");
		field.put("type", "text");
		attributes.add(field);
		
		field = new HashMap<String, Object>();
		field.put("name", "create_date");
		field.put("label", "创建时间");
		field.put("type", "date");
		attributes.add(field);
		
		field = new HashMap<String, Object>();
		field.put("name", "modify_date");
		field.put("label", "编辑时间");
		field.put("type", "date");
		attributes.add(field);
		
		request.setAttribute("attributes", attributes);
		List<Map<String,Object>> queryattributes = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> map : attributes) {
			if(!map.get("name").toString().endsWith("date")) {
				queryattributes.add(map);
			}
		}
		request.setAttribute("queryattributes", queryattributes);
		return "admin/user";
	}
	
	@RequestMapping("role")
	public String role(HttpServletRequest request,Model model) {
		request.setAttribute("datatitle", "角色");
		request.setAttribute("datatype", "sys_role");
		
		List<Map<String,Object>> attributes = new ArrayList<Map<String,Object>>();
		Map<String,Object> field = new HashMap<String, Object>();
		field.put("name", "name");
		field.put("label", "角色名");
		field.put("type", "text");
		attributes.add(field);
		
		field = new HashMap<String, Object>();
		field.put("name", "desc");
		field.put("label", "角色描述");
		field.put("type", "text");
		attributes.add(field);		
		
		request.setAttribute("attributes", attributes);
		List<Map<String,Object>> queryattributes = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> map : attributes) {
			if(!map.get("name").toString().endsWith("date")) {
				queryattributes.add(map);
			}
		}
		request.setAttribute("queryattributes", queryattributes);
		return "admin/role";
	}
	
	@RequestMapping("object")
	public String object(HttpServletRequest request,Model model) {
		request.setAttribute("datatitle", "对象管理");
		request.setAttribute("datatype", "sys_role");
		return "admin/object";
	}
	
	@RequestMapping("object/getBaseInfo")
	public String getBaseInfo(HttpServletRequest request, Model model, @RequestParam(value = "id", required = true) String id) {
		Optional<SysObject>  findResult = sysObjectDao.findById(id);
		SysObject obj = findResult.get();
		List<SysField> fields = sysFieldDao.findByParentObjAndScope(obj,"system");
		request.setAttribute("systemattrs", fields);
		
		fields = sysFieldDao.findByParentObjAndScope(obj,"custom");
		request.setAttribute("customattrs", fields);
		String url = request.getContextPath() + request.getServletPath().replace("/getBaseInfo", "");
		model.addAttribute("currentObject", obj);
		model.addAttribute("currentPath", url);
		return "admin/object/baseinfo";
	}
}
